home *** CD-ROM | disk | FTP | other *** search
Wrap
RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) NNNNaaaammmmeeee RWTPtrHashDictionary<K,V> - Rogue Wave library class SSSSyyyynnnnooooppppssssiiiissss #include <rw/tphdict.h> unsigned hashFun(const K&); RWTPtrHashDictionary<K,V> dictionary(hashFun); PPPPlllleeeeaaaasssseeee NNNNooootttteeee!!!! IIIIffff yyyyoooouuuu ddddoooo nnnnooootttt hhhhaaaavvvveeee tttthhhheeee SSSSttttaaaannnnddddaaaarrrrdddd CCCC++++++++ LLLLiiiibbbbrrrraaaarrrryyyy,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ddddeeeessssccccrrrriiiibbbbeeeedddd hhhheeeerrrreeee.... OOOOtttthhhheeeerrrrwwwwiiiisssseeee,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ttttoooo RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhMMMMaaaapppp described in the Class Reference. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<<<<KKKK,,,,VVVV>>>> is a dictionary of keys of type KKKK and values of type VVVV, implemented using a hash table. While duplicates of values are allowed, duplicates of keys are not. It is a ppppooooiiiinnnntttteeeerrrr based collection: pointers to the keys and values are copied in and out of the hash buckets. Parameters KKKK and VVVV represent the type of the key and the type of the value, respectively, to be inserted into the table. These can be either classes or fundamental types. Class KKKK must have well-defined equality semantics (KKKK::::::::ooooppppeeeerrrraaaattttoooorrrr========((((ccccoooonnnnsssstttt KKKK&&&&))))). Class VVVV can be of any type.A user-supplied hashing function for type KKKK must be supplied to the constructor when creating a new table. If KKKK is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RRRRWWWWCCCCSSSSttttrrrriiiinnnngggg, RRRRWWWWDDDDaaaatttteeee, RRRRWWWWTTTTiiiimmmmeeee, and RRRRWWWWWWWWSSSSttttrrrriiiinnnngggg contain static member functions called hhhhaaaasssshhhh that can be supplied to the constructor as is. The function must have prototype: unsigned hhhhFFFFuuuunnnn(const K& a); and should return a suitable hash value for the object aaaa. To find a value, the key is first hashed to determine in which bucket the key and value can be found. The bucket is then searched for an object that is equal (as determined by the equality operator) to the key. The initial number of buckets in the table is set by the constructor. There is a default value. If the number of (key/value) pairs in the collection greatly exceeds the number of buckets then efficiency will sag because PPPPaaaaggggeeee 1111 RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) each bucket must be searched linearly. The number of buckets can be changed by calling member function rrrreeeessssiiiizzzzeeee(((()))). This is relatively expensive because all of the keys must be rehashed. If you wish for this to be done automatically, then you can subclass from this class and implement your own special iiiinnnnsssseeeerrrrtttt(((()))) and rrrreeeemmmmoooovvvveeee(((()))) functions which perform a rrrreeeessssiiiizzzzeeee(((()))) as necessary. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee None EEEExxxxaaaammmmpppplllleeee #include <rw/tphdict.h> #include <rw/cstring.h> #include <rw/rwdate.h> #include <rw/rstream.h> main() { RWTPtrHashDictionary<RWCString, RWDate> birthdays(RWCString::hash); birthdays.insertKeyAndValue (new RWCString("John"), new RWDate(12, "April", 1975) ); birthdays.insertKeyAndValue (new RWCString("Ivan"), new RWDate(2, "Nov", 1980) ); // Alternative syntax: birthdays[new RWCString("Susan")] = new RWDate(30, "June", 1955); birthdays[new RWCString("Gene")] = new RWDate(5, "Jan", 1981); // Print a birthday: RWCString key("John"); cout << *birthdays[&key] << endl; birthdays.clearAndDestroy(); return 0; } Program output: April 12, 1975 PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(unsigned (*hashKey)(const K&), size_t buckets = RWDEFAULT_CAPACITY); Constructs an empty hash dictionary. The first argument is a pointer to PPPPaaaaggggeeee 2222 RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) a user-defined hashing function for items of type KKKK (the key). The table will initally have bbbbuuuucccckkkkeeeettttssss buckets although this can be changed with member function rrrreeeessssiiiizzzzeeee(((()))). RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(const RWTPtrHashDictionary<K,V>& c); Constructs a new hash dictionary as a shallow copy of cccc. After construction, pointers will be shared between the two collections. The new object will use the same hashing function and have the same number of buckets as cccc. Hence, the keys will not be rehashed. PPPPuuuubbbblllliiiicccc OOOOppppeeeerrrraaaattttoooorrrrssss RWTPtrHashDictionary<K,V>& ooooppppeeeerrrraaaattttoooorrrr====(const RWTPtrHashDictionary<K,V>& c); Sets self to a shallow copy of cccc. Afterwards, pointers will be shared between the two collections. Self will use the same hashing function and have the number of buckets as cccc. Hence, the keys will not be rehashed. V*& ooooppppeeeerrrraaaattttoooorrrr[[[[]]]](K* key); Look up the key kkkkeeeeyyyy and return a reference to the pointer of its associated value. If the key is not in the dictionary, then it is added to the dictionary. In this case, the pointer to the value will be undefined. Because of this, if there is a possibility that a key will not be in the dictionary, then this operator can only be used as an llllvvvvaaaalllluuuueeee. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss void aaaappppppppllllyyyyTTTTooooKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee( void (*applyFun)(K*,V*&,void*),void* d); Applies the user-defined function pointed to by aaaappppppppllllyyyyFFFFuuuunnnn to every key- value pair in the dictionary. This function must have prototype: void yyyyoooouuuurrrrFFFFuuuunnnn(K* key, V*& value, void* d); This function will be called for each key value pair in the dictionary, with a pointer to the key as the first argument and a reference to a pointer to the value as the second argument. The key should not be changed or touched. A new value can be substituted, or the old value can be changed. Client data may be passed through as parameter dddd. PPPPaaaaggggeeee 3333 RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) void cccclllleeeeaaaarrrr(); Removes all key value pairs from the collection. void cccclllleeeeaaaarrrrAAAAnnnnddddDDDDeeeessssttttrrrrooooyyyy(); Removes all key value pairs from the collection aaaannnndddd deletes both the keys and the values. RWBoolean ccccoooonnnnttttaaaaiiiinnnnssss(const K* key) const; Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to the key pointed to by kkkkeeeeyyyy. Returns FFFFAAAALLLLSSSSEEEE otherwise. Equality is measured by the class-defined equality operator for type KKKK. size_t eeeennnnttttrrrriiiieeeessss() const; Returns the number of key-value pairs currently in the dictionary. K* ffffiiiinnnndddd(const K* key) const; Returns a pointer to the kkkkeeeeyyyy which is equal to the key pointed to by kkkkeeeeyyyy, or nnnniiiillll if no such item could be found. Equality is measured by the class-defined equality operator for type KKKK. V* ffffiiiinnnnddddVVVVaaaalllluuuueeee(const K* key) const; Returns a pointer to the vvvvaaaalllluuuueeee associated with the key pointed to by kkkkeeeeyyyy, or nnnniiiillll if no such item could be found. Equality is measured by the class-defined equality operator for type KKKK. K* ffffiiiinnnnddddKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const K* key, V*& retVal) const; Returns a pointer to the kkkkeeeeyyyy associated with the key pointed to by kkkkeeeeyyyy, or nnnniiiillll if no such item could be found. If a key is found, the pointer to its associated value is put in rrrreeeettttVVVVaaaallll. Equality is measured by the class-defined equality operator for type KKKK. PPPPaaaaggggeeee 4444 RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTPPPPttttrrrrHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) void iiiinnnnsssseeeerrrrttttKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(K* key, V* value); If the key pointed to by kkkkeeeeyyyy is in the dictionary, then its associated value is changed to vvvvaaaalllluuuueeee. Otherwise, a new key value pair is inserted into the dictionary. RWBoolean iiiissssEEEEmmmmppppttttyyyy() const; Returns TTTTRRRRUUUUEEEE if the dictionary has no items in it, FFFFAAAALLLLSSSSEEEE otherwise. K* rrrreeeemmmmoooovvvveeee(const K* key); Removes the key and value pair where the key is equal to the key pointed to by kkkkeeeeyyyy. Returns the kkkkeeeeyyyy or nnnniiiillll if no match was found. Equality is measured by the class-defined equality operator for type KKKK. void rrrreeeessssiiiizzzzeeee(size_t N); Changes the number of buckets to NNNN. This will result in all of the keys being rehashed. PPPPaaaaggggeeee 5555